package com.ccn.ui.a1ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import com.ccn.ui.R;

public class A1_2_6PathMesureView extends View {
    private static final String TAG = "A1_2_6PathMesureView";
    private Paint mPaint = new Paint();
    private Paint mLinePaint = new Paint();//坐标系

    private Bitmap mBitmap;

    public A1_2_6PathMesureView(Context context) {
        this(context, null);
    }

    public A1_2_6PathMesureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public A1_2_6PathMesureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(4);

        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setColor(Color.RED);
        mLinePaint.setStrokeWidth(6);

        /*
        BitmapFactory.decodeResource会导致mBitmap为null
         */
//        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_baseline_keyboard_arrow_right_24);
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        TypedValue value = new TypedValue();
//        context.getResources().openRawResource(resId, value);
//        options.inTargetDensity = value.density;
//        options.inScaled=false;//不缩放
//        options.inSampleSize = 4;//缩小4倍
        Drawable drawable = getContext().getDrawable(R.drawable.ic_baseline_keyboard_arrow_right_24);
        mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mBitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, mLinePaint);
        canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), mLinePaint);

        canvas.translate(getWidth() / 2, getHeight() / 2);

        /*Path path = new Path();
        path.lineTo(0, 200);
        path.lineTo(200, 200);
        path.lineTo(200, 0);

        *//*
        需要关联创建好的path才可以使用
        forceClosed会影响path的测量结果
         *//*
        PathMeasure pathMeasure = new PathMeasure();
        pathMeasure.setPath(path, true);
        Log.e(TAG, "onDraw: forceClosed = true" + pathMeasure.getLength());

        PathMeasure pathMeasure2 = new PathMeasure();
        pathMeasure2.setPath(path, false);
        Log.e(TAG, "onDraw: forceClosed = false" + pathMeasure2.getLength());

        PathMeasure pathMeasure1 = new PathMeasure(path,false);
        Log.e(TAG, "onDraw: forceClosed = false" + pathMeasure1.getLength());

        path.lineTo(200,-200);
        Log.e(TAG, "onDraw: forceClosed = false" + pathMeasure1.getLength());

        *//*
        如果path进行了调整，需要重新调用setPath进行关联
         *//*
        pathMeasure1.setPath(path,false);
        Log.e(TAG, "onDraw: forceClosed = false" + pathMeasure1.getLength());*/

        /*Path path = new Path();
        path.addRect(-200, -200, 200, 200, Path.Direction.CW);

        Path dst = new Path();
        dst.lineTo(-300,-300);//添加一条直线

        PathMeasure pathMeasure = new PathMeasure(path, false);
        *//*
        true  截取位置为起始点
        false 之前线段结束位置为起始点,并从起始点连接截取位置起始点
         *//*
        pathMeasure.getSegment(200, 1000, dst, false);


        canvas.drawPath(path, mPaint);
        canvas.drawPath(dst,mLinePaint);*/
        /*
        nextContour跳转到下一个path
         */
        /*Path path = new Path();
        //添加一个矩形
        path.addRect(-100,-100,100,100, Path.Direction.CW);
        //添加一个椭圆
        path.addOval(-200,-200,200,200, Path.Direction.CW);

        canvas.drawPath(path,mPaint);

        PathMeasure pathMeasure = new PathMeasure(path,false);
        Log.e(TAG, "onDraw: forceClosed = false" + pathMeasure.getLength());
        //跳转到下一个path
        pathMeasure.nextContour();
        Log.e(TAG, "onDraw: forceClosed = false" + pathMeasure.getLength());*/

        mPath.reset();

        mPath.addCircle(0, 0, 200, Path.Direction.CW);
        canvas.drawPath(mPath, mPaint);

        mFloat += 0.01;
        if (mFloat >= 1) {
            mFloat = 0;
            canvas.drawPath(mPath, mPaint);
        }

        /*PathMeasure pathMeasure = new PathMeasure(mPath, false);
        pathMeasure.getPosTan(*//*pathMeasure.getLength() * 5 / 6*//*pathMeasure.getLength() * mFloat, pos, tan);

        Path dst = new Path();
        pathMeasure.getSegment(0, pathMeasure.getLength() * mFloat, dst, true);
        canvas.drawPath(dst, mLinePaint);
        *//*
        pos[0]，就是点x轴坐标
        pos[1]，就是点y轴坐标
        tan值不好理解，值是取自半径为1的单位圆上的坐标
        tan[0]，单位圆上点x轴坐标，其实就是角对边的边长
        tan[1]，单位圆上点y轴坐标，邻边的边长
        图tan.png仅仅表示夹角tan值，与实际切线与x轴夹角并无直接关系
        *//*
        Log.e(TAG, "onDraw: " + String.format("pos[0]:%f pos[1]:%f", pos[0], pos[1]));
        Log.e(TAG, "onDraw: " + String.format("tan[0]:%f tan[1]:%f", tan[0], tan[1]));

        //计算出当前切线与x轴夹角的度数
        double degree = Math.atan2(tan[1], tan[0]) * 180 / Math.PI;
        Log.e(TAG, "onDraw: " + degree);

        mMatrix.reset();
        //进行角度旋转
        mMatrix.postRotate((float) degree, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
        //将图片的绘制点中心与当前点重合
        mMatrix.postTranslate(pos[0] - mBitmap.getWidth() / 2, pos[1] - mBitmap.getHeight() / 2);
        canvas.drawBitmap(mBitmap, mMatrix, mPaint);*/

        PathMeasure pathMeasure = new PathMeasure(mPath, false);
        //将位置信息和tangent信息保存在matrix里面
        pathMeasure.getMatrix(pathMeasure.getLength() * mFloat, mMatrix,
                PathMeasure.POSITION_MATRIX_FLAG | PathMeasure.TANGENT_MATRIX_FLAG);
        //将图片的旋转坐标调整到图片中心位置
        mMatrix.preTranslate(-mBitmap.getWidth() / 2, -mBitmap.getHeight() / 2);
        canvas.drawBitmap(mBitmap, mMatrix, mPaint);

        invalidate();
    }

    private Matrix mMatrix = new Matrix();
    /**
     * pos[0]，就是点x轴坐标
     * pos[1]，就是点y轴坐标
     */
    private float[] pos = new float[2];
    /**
     * tan[0]，单位圆上点x轴坐标，其实就是角对边的边长
     * tan[1]，单位圆上点y轴坐标，邻边的边长
     */
    private float[] tan = new float[2];
    private Path mPath = new Path();
    private float mFloat;
}
